//
//  LandController.m
//  quickLook
//
//  Created by J on 15/9/3.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "LandController.h"
#import "LandController2.h"
#import "Header.h"
#import "ProgressHUD.h"

#import "IndividualViewController.h"
@interface LandController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *passWord;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *leftBarbutton;

@property (weak, nonatomic) IBOutlet UIButton *Register;
@property (weak, nonatomic) IBOutlet UIButton *Land;
@property (weak, nonatomic) IBOutlet UINavigationBar *navgationbar;

@end

@implementation LandController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self addTapgesture];
    [self createUI];

}

- (void)addTapgesture{

    UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(endEditing:)];
    
    [self.view addGestureRecognizer:tap];



}
- (void)endEditing:(UITapGestureRecognizer*)tap{

    [self.view endEditing:YES];

}


- (IBAction)dismiss:(id)sender {
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)createUI
{

    _passWord.delegate=self;
    
    _navgationbar.frame=CGRectMake(0, 0, self.view.frame.size.width, 64);
    [_navgationbar setBackgroundImage:[UIImage imageNamed:@"navyellowBG.png"] forBarMetrics:UIBarMetricsDefault];
//    navyellowBG.png
    

    //右边按钮颜色
    
    UIImage* image=[[[UIImage imageNamed:@"backbuttonX"] TransformtoSize:CGSizeMake(20, 20)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

   
    
    [_leftBarbutton setImage:image];
    self.title=@"登陆";
    _Land.layer.cornerRadius=3;
    
    
    
    
    
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    
    NSDictionary* dic =[userdefault objectForKey:DATADIC];
    
    _dataDic=dic;
    _Register.enabled=YES;
    
    
    
    [userdefault synchronize];
    
    
    
    
    if ((_phoneNumber.text&&_passWord.text)||(dic[@"phoneNumber"]&&dic[@"passWord"])) {
        
        
        [self Land];
        
        _Register.enabled=NO;
        
    }
    
    _passWord.delegate=self;


}

//登录
- (IBAction)land:(UIButton *)sender {
    
    
    if (_phoneNumber.text.length!=0&&_passWord.text.length!=0) {
     

        
        if (![self isMobileNumber:_phoneNumber.text])//检测密码长度
        {
            
            
            
            [ProgressHUD showError:@"电话号码输入有误" Interaction:YES];
            
            
        }else if(_passWord.text.length<8)
        {


            
            [ProgressHUD showError:@"密码长度少于8个字节" Interaction:YES];

        }else
        {
            
            
            
            
            //登录
            [self landProgress];
            [ProgressHUD show:@"登录中..." Interaction:YES];

        
        
        
        }
    }
    
    [self dismiss];

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    CGFloat offset=self.view.frame.size.height-(textField.frame.origin.y+textField.frame.size.height+216+50);
    
    
    if (offset<=0) {
        CGRect frame =self.view.frame;
        frame.origin.y=offset;
        self.view.frame=frame;
        
        
    }
    
    return YES;


}


- (void)landProgress{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *params = @{@"phone":_phoneNumber.text, @"password":_passWord.text};
    JZJLog(@"%@%@",_passWord,_passWord.text);
    
    [manager POST:@"http://api.kuaikanmanhua.com/v1/phone/signin" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString* result= responseObject[@"message"];
        
        
        if ([result isEqualToString:@"OK"]) {
            
            
            
            NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
            
            [userdefault setObject:responseObject[@"data"] forKey:DATADIC];
            
            [userdefault setBool:YES forKey:ISLAND];
            
            [userdefault synchronize];
            
            //跳回登录前页面
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
            [self.navigationController popViewControllerAnimated:YES];
            
            
            [ProgressHUD showSuccess:@"成功" Interaction:YES];
            
            
        }else  if([result isEqualToString:@"invalid user or password"])  //如果登录失败 提示错误信息
        {
            
            
            
            [ProgressHUD showError:@"账号或密码输入有误" Interaction:YES];

            
        }else
        {
        
            [ProgressHUD showError:@"输入有误" Interaction:YES];

        
        }
        
   
        [self dismiss];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        JZJLog(@"Error: %@", error);
        
        
    }];
    
    
    
    
}


//检测电话号码合法性
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


//提示框消失
- (void)dismiss{
    
    [UIView animateWithDuration:1.5 animations:^{
        
        [ProgressHUD dismiss];

        
    }];



}




//注册跳转
- (IBAction)Register:(UIButton*)sender {
    
    
    LandController2 * land=[[LandController2 alloc]initWithNibName:@"LandController2" bundle:nil];
    
    land.hidesBottomBarWhenPushed=YES;

    land.navigationItem.title=@"注册";

    
    
    [self.navigationController pushViewController:land animated:YES];



    
    
}
- (IBAction)forgetPassWord:(id)sender {
    
    
    
    
    
    LandController2 * land=[[LandController2 alloc]initWithNibName:@"LandController2" bundle:nil];
    
    land.hidesBottomBarWhenPushed=YES;
    
    land.navigationItem.title=@"忘记密码";
    [self.navigationController pushViewController:land animated:YES];
    
    

    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
